
/*-----------------------------------------------*
Name: Haqdarshak Pilot Intervention-Analysis (for Appendix)
Date Created: 30 July, 2018
Date Last Modified: 28 April, 2021
Created by: Aaron Berman and modified by Daniela Paz and Saumya Mathur on Nov.13/2020 
Uses Data: individual_data_documents.dta
Creates Data: Latex Tables for Appendix
Description: Creates documents table
*-------------------------------------------------*/



*..................................................
**Install user-written commands
foreach package in mdesc nmissing veracrypt {
     capture which `package'
	 if _rc==111 ssc install `package'
}
*..................................................
clear all
set more off
version 12.0
cap log close
pause off


  


//open log 
*cd "$logs"
*log using "analysis_other_tables.smcl", replace

local figure1 = 1
local crossprice = 0


	//start with baseline household data 
	use "$pilot/individual_data_documents", clear

	//count each scheme
	eststo clear 
	matrix documents = J(6, 4, .)
	matrix rownames documents = aadhaar_card_ pan_card_ voter_id_ ration_card_ residential_certificate_ bank_account_

	forv i = 1/2 {
		forv j = 1/6 {
			local card: word `j' of "aadhaar_card_" "pan_card_" "voter_id_" "ration_card_" "residential_certificate_" "bank_account_"
			count if relation_to_respondent_ == `i' 
			local num_person = `r(N)'
			count if `card' == 1 & relation_to_respondent_ == `i'
			local num_card = `r(N)'
			local pct: di %8.3f `num_card' / `num_person'
			matrix documents[`j', `i'] = `pct'
		}

	}

	forv i = 3/4 {
		forv j = 1/6 {
			local card: word `j' of "aadhaar_card_" "pan_card_" "voter_id_" "ration_card_" "residential_certificate_" "bank_account_"
			count if relation_to_respondent_ == 1 & above_48000 == `i' - 3
			local num_person = `r(N)'
			count if `card' == 1 & relation_to_respondent_ == 1 & above_48000 == `i' - 3
			local num_card = `r(N)'
			local pct: di %8.3f `num_card' / `num_person'
			matrix documents[`j', `i'] = `pct'
		}
	}



	//output table
	cd "$Main"
	#delimit ;
	esttab matrix(documents) using "Table_1.tex",
		label booktabs
		collabels("Head of Household" "Spouse" "Head of Household, Income < Rs. 48,000" "Head of Household, Income $/geq$ Rs. 48,000")
		varlabels(aadhaar_card_ "Aadhaar Card" pan_card_ "PAN Card" voter_id_ "Voter ID" ration_card_ "Ration Card" residential_certificate_ "Residential Certificate" bank_account_ "Bank Account") 
		mlabels(none)
		replace;
	#delimit cr



	//go back to baseline household data 
	use "$pilot/hh_data_treatment_b", clear 

	*Ration Cards
	label define ration 1 "Does Not Have" 2 "BPL (Pink) Card" 3 "APL (Blue/Green) Card" 4 "Antayodaya Anna Yojana (Yellow) Card"
	label values ration_card_type ration

	//tabulate card type 
	estpost tab ration_card_type

	//output to latex 
	//output table
	cd "$Appe"
	#delimit ;
	esttab using "Table_A3.tex",
		label booktabs
		mlabels("Number of HHs")
		varlabels(`e(labels)')
		varwidth(50)
		nonumbers 
		drop(Total) 
		nonotes
		nogaps
		fragment
		replace;
	#delimit cr 


	*Economic Disruptions
	egen num_disruptions = rowtotal(E_death_head_of_hh-E_dropinincome_cropfailure)

	//tab number of disruptions 
	estpost tab num_disruptions

	//output to latex 
	//output table
	cd "$Appe"
	#delimit ;
	esttab using "Table_A5.tex",
		label booktabs
		mlabels("Number of HHs")
		varlabels(`e(labels)')
		varwidth(50)
		nonumbers 
		drop(Total) 
		nonotes
		nogaps
		fragment
		replace;
	#delimit cr 	

/* Aggregated this data in order to protect respondent privacy; the latest version of table A6 was created in July 2021 using this code

	*Networks 
	matrix networks = J(6,5,.)

	forv i = 1/6 {
		forv j = 1/5 {
			local position: word `j' of "sarpanch" "panchayat_sec" "bdo" "district_collector" "mla"
			local condition: word `i' of "relation_to_`position' == 0" "relation_to_`position' == 1" "relation_to_`position' == 2" "relation_to_`position' == 3" "spoken_to_`position' == 1" "help_from_`position' == 1"

			count if `condition'
			matrix networks[`i', `j'] = `r(N)'

		}
	}


	//output table
	cd "$Appe"
	#delimit ;
	esttab matrix(networks) using "Table_A6.tex",
		label booktabs
		collabels("Sarpanch" "GP Secretary" "Block Development Officer" "District Collector" "MLA")
		varlabels(r1 "No Relationship" r2 "Close Friend" r3 "Member of Household" r4 "Neighbor" r5 "/midrule Ever Spoken To" r6 "Ever Asked Help From") 
		mlabels(none)
		replace;
	#delimit cr

*/
	*Applying for Gov't Schemes 
	label define yesno 0 "No" 1 "Yes" 998 "Do Not Know" 999 "No Answer"
	label define experience 1 "Very Difficult" 2 "Difficult" 3 "Easy" 4 "Very Easy" 998 "Do Not Know" 999 "No Answer"

	label values applied_for_govt_scheme yesno
	label values rate_experience experience 
	label values applied_rejected yesno

	//tab number of applications
	estpost tab applied_for_govt_scheme

	//output to latex 
	//output table
	cd "$latex"
	#delimit ;
	esttab using "Govt Scheme Applications.tex",
		label booktabs
		mlabels("Number of HHs")
		varlabels(`e(labels)')
		varwidth(50)
		nonumbers 
		drop(Total) 
		nonotes
		nogaps
		fragment
		replace;
	#delimit cr 	

	//tab experience
	estpost tab rate_experience

	//output to latex 
	//output table
	cd "$latex"
	#delimit ;
	esttab using "Govt Scheme Experience.tex",
		label booktabs
		mlabels("Number of HHs")
		varlabels(`e(labels)')
		varwidth(50)
		nonumbers 
		drop(Total) 
		nonotes
		nogaps
		fragment
		replace;
	#delimit cr 	

	//tab applications 
	estpost tab applied_rejected 

	//output table
	cd "$latex"
	#delimit ;
	esttab using "Govt Scheme Rejected.tex",
		label booktabs
		mlabels("Number of HHs")
		varlabels(`e(labels)')
		varwidth(50)
		nonumbers 
		drop(Total) 
		nonotes
		nogaps
		fragment
		replace;
	#delimit cr 	



	*Value Questions
	label values inelligible_household yesno 
	label values inelligible_individual yesno 
	label values comfort_govtoffice_alone yesno
	label define satisfied 1 "Very Satisfied" 2 "Satisfied" 3 "Unsatisfied" 4 "Very Unsatisfied" 998 "Don't Know" 999 "No Answer"
	label values satisfied_grampanchayat satisfied 
	label values satisfied_stategovt satisfied 
	label values difficulty_in_application experience

	rename inelligible_household ineligible_household

	local valcount = 0
	foreach x of varlist ineligible_household inelligible_individual difficulty_in_application comfort_govtoffice_alone satisfied_grampanchayat satisfied_stategovt {
		eststo clear 
		if "`x'" == "comfort_govtoffice_alone" {
			estpost tab `x' member_gender_1
			local options = "unstack noobs mlabels(none)"
		}
		else {
			estpost tab `x'
			local options = `"mlabels("Number of HHs")"'
		}
		local valcount = `valcount' + 1

		cd "$latex"
		#delimit ;
		esttab using "Values `valcount'.tex",
			label booktabs `options'
			varlabels(`e(labels)')
			varwidth(50)
			nonumbers 
			drop(Total) 
			nonotes
			nogaps
			fragment
			replace;
		#delimit cr

	}


	*Political Participation 
	label values member_political_party yesno
	label values vote_panchayat_elections yesno
	label values vote_state_elections yesno 
	foreach x of varlist member_political_party vote_panchayat_elections vote_state_elections {
		estpost tab `x' 
		cd "$latex"
		#delimit ;
		esttab using "`x'.tex",
			label booktabs
			mlabels("Number of HHs")
			varlabels(`e(labels)')
			varwidth(50)
			nonumbers 
			drop(Total) 
			nonotes
			nogaps
			fragment
			replace;
		#delimit cr

	}




